<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * @property int $id
 * @property string $room_num
 * @property int $room_id
 * @property string $user_name
 * @property string $openid
 * @property boolean $sex
 * @property string $user_phone
 * @property string $check_code
 * @property string $in_date
 * @property string $out_date
 * @property int $status
 * @property string $updated_at
 * @property string $created_at
 */
class RoomCheckIn extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'room_check_in';

    /**
     * @var array
     */
    protected $fillable = ['room_num', 'room_id', 'user_name', 'openid', 'sex', 'user_phone', 'check_code', 'in_date', 'out_date','status', 'updated_at', 'created_at'];

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public function rooms(){
        return $this->hasOne(Rooms::class,"room_num","room_num");
    }


    /**
     * @param $openId
     * @return Model|\Illuminate\Database\Query\Builder|object|null|RoomCheckIn
     */
    public function getLiving($openId)
    {
        $date = date('Y-m-d');
        return DB::table($this->table)
            ->where('status', 1)
            ->where('openid', $openId)
            ->where('out_date', '<=', $date)
            ->first();
    }

    public function getDetail($openId)
    {
        $columns = [
            'room_check_in.id',
            'room_check_in.check_code',
            'room_check_in.in_date',
            'room_check_in.out_date',
            'room_check_in.status',
            'rooms.room_num',
            'rooms.room_name',
            'rooms.desc',
            'rooms.floor',
            'rooms.wifi_name',
            'rooms.wifi_pass',
        ];
        $date = date('Y-m-d');
        return DB::table($this->table)
            ->leftJoin('rooms', $this->table . '.room_num', '=', 'rooms.room_num')
            ->where('room_check_in.openid', $openId)
            ->where('out_date', '<=', $date)
            ->first($columns);
    }
}
